﻿using Abp.AspNetCore.Mvc.Controllers;
using Batman.Cms.Core.Core.Attributes;
using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Helpers;
using System.Net;
using System.Web;

namespace Batman.Jianbei.Forum.Controllers.Weixins
{
    public class WeixinController : AbpController
    {
        /// <summary>
        /// 获取JSSDK所需要的配置信息
        /// </summary>
        /// <param name="url">前端web URL</param>
        /// <returns></returns>
        [AcceessOriginal]
        public ActionResult Web(string url)
        {
            var appId = Config.WeixinSetting.weixin_app_id;
            var timestamp = JSSDKHelper.GetTimestamp();
            var nonceStr = JSSDKHelper.GetNoncestr();
            string ticket = JsApiTicketContainer.TryGetJsApiTicket(appId, Config.WeixinSetting.weixin_secret);

            var signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, url);

            var state = "my-state"; // 用于验证
            var authorizeUrl = OAuthApi.GetAuthorizeUrl(appId, url, state, OAuthScope.snsapi_userinfo);

            return Json(new { timestamp, nonceStr, signature, appId, authorizeUrl });
        }

       
    }
}
